<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_ps_fee.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

  class ot_ps_fee {
    var $title, $output;

    function __construct() {
    	global $price, $db;
      $this->code = 'ot_ps_fee';
      $this->title = MODULE_ORDER_TOTAL_PS_FEE_TITLE;
      $this->description = MODULE_ORDER_TOTAL_PS_FEE_DESCRIPTION;
      $this->enabled = ((MODULE_ORDER_TOTAL_PS_FEE_STATUS == 'true') ? true : false);
      $this->sort_order = MODULE_ORDER_TOTAL_PS_FEE_SORT_ORDER;
      $this->output = array();
      $this->db = $db;
    }

    function process() {
      global $order, $price, $ps_cost, $ps_country, $shipping;
      $customer_id = $_SESSION['customer_id'];

      if (MODULE_ORDER_TOTAL_PS_FEE_STATUS == 'true') {

        $ps_country = false;


				$num = $this->db->db_query("SELECT COUNT(*) AS count from " . TABLE_CUSTOMERS_BASKET . " cb, " . TABLE_PRODUCTS . " p  WHERE cb.customers_id = '" . $customer_id . "' and cb.products_id = p.products_id and p.products_fsk18 = '1'");
				$age = $num->fields['count'];


        if ($age > '0') {
          //process installed shipping modules
          if ($_SESSION['shipping']['id'] == 'flat_flat') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_FLAT);
          if ($_SESSION['shipping']['id'] == 'item_item') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_ITEM);
          if ($_SESSION['shipping']['id'] == 'table_table') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_TABLE);
          if ($_SESSION['shipping']['id'] == 'zones_zones') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_ZONES);
          if ($_SESSION['shipping']['id'] == 'ap_ap') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_AP);
          if ($_SESSION['shipping']['id'] == 'dp_dp') $ps_zones = explode(",", MODULE_ORDER_TOTAL_PS_FEE_DP);

            for ($i = 0; $i < count($ps_zones); $i++) {
            	$cod_z = explode(':', $ps_zones[$i]);
            	 for ($ii = 0; $ii < count($cod_z); $ii++) {
					if ($cod_z[$ii] == $order->delivery['country']['iso_code_2']) {
						$cod_cost = $cod_z[$ii + 1];
						$cod_country = true;
						break;
					} elseif ($cod_zones[$ii] == '00') {
						$cod_cost = $cod_z[$ii + 1];
						$cod_country = true;
						break;
					}
				}
            }
          } else {
            //PS selected, but no shipping module which offers PS
          }

        if ($ps_country) {

            $ps_tax = get_tax_rate(MODULE_ORDER_TOTAL_PS_FEE_TAX_CLASS, $order->delivery['country']['id'], $order->delivery['zone_id']);
            $ps_tax_description = get_tax_description(MODULE_ORDER_TOTAL_PS_FEE_TAX_CLASS, $order->delivery['country']['id'], $order->delivery['zone_id']);
        if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 1) {
            $order->info['tax'] += add_tax($ps_cost, $ps_tax)-$ps_cost;
            $order->info['tax_groups'][TAX_ADD_TAX . "$ps_tax_description"] += add_tax($ps_cost, $ps_tax)-$ps_cost;
            $order->info['total'] += $ps_cost + (add_tax($ps_cost, $ps_tax)-$ps_cost);
            $ps_cost_value= add_tax($ps_cost, $ps_tax);
            $ps_cost= $price->format($ps_cost_value,true);
        }
        if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
            $order->info['tax'] += add_tax($ps_cost, $ps_tax)-$ps_cost;
            $order->info['tax_groups'][TAX_NO_TAX . "$ps_tax_description"] += add_tax($ps_cost, $ps_tax)-$ps_cost;
            $ps_cost_value=$ps_cost;
            $ps_cost= $price->format($ps_cost,true);
            $order->info['subtotal'] += $ps_cost_value;
            $order->info['total'] += $ps_cost_value;
        }
        if (!$ps_cost_value) {
           $ps_cost_value=$ps_cost;
           $ps_cost= $price->format($ps_cost,true);
           $order->info['total'] += $ps_cost_value;
        }
            $this->output[] = array('title' => $this->title . ':',
                                    			'text' => $ps_cost,
                                    			'value' => $ps_cost_value);
        } 
      }
    }

    function check() {
      if (!isset($this->_check)) {
        $check = $this->db->db_query("SELECT configuration_value from " . TABLE_CONFIGURATION . " WHERE configuration_key = 'MODULE_ORDER_TOTAL_PS_FEE_STATUS'");
        $this->_check = $check->_numOfRows;
      }
      return $this->_check;
    }

    function keys() {
      return array('MODULE_ORDER_TOTAL_PS_FEE_STATUS', 'MODULE_ORDER_TOTAL_PS_FEE_SORT_ORDER', 'MODULE_ORDER_TOTAL_PS_FEE_FLAT', 'MODULE_ORDER_TOTAL_PS_FEE_ITEM', 'MODULE_ORDER_TOTAL_PS_FEE_TABLE', 'MODULE_ORDER_TOTAL_PS_FEE_ZONES', 'MODULE_ORDER_TOTAL_PS_FEE_AP', 'MODULE_ORDER_TOTAL_PS_FEE_DP', 'MODULE_ORDER_TOTAL_PS_FEE_TAX_CLASS');
    }

    function install() {
      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_STATUS', 'true', '6', '0', 'cfg_select_option(array(\'true\', \'false\'), ', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_SORT_ORDER', '35', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_FLAT', 'AT:3.00,DE:3.58,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_ITEM', 'AT:3.00,DE:3.58,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_TABLE', 'AT:3.00,DE:3.58,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_ZONES', 'CA:4.50,US:3.00,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_AP', 'AT:3.63,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_DP', 'DE:4.00,00:9.99', '6', '0', now())");

      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, use_function, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_PS_FEE_TAX_CLASS', '0', '6', '0', 'get_tax_class_title', 'cfg_pull_down_tax_classes(', now())");
    }

    function remove() {
      $this->db->db_query("DELETE FROM " . TABLE_CONFIGURATION . " WHERE configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
  }
?>